from fairy_server import models
from django.http import JsonResponse
from django.forms.models import model_to_dict
import json

# Create your views here.
# 获取分类列表
def fairy_category_list(request):
    data = models.FairyCategory.objects.only()

    dataResponse = []
    for single in data:
        single = model_to_dict(single)
        dataResponse.append(single)
    return JsonResponse(dataResponse, safe=False)


# 通过分类id查询列表
def fairy_detail_list(request):
    raw_data = request.body.decode('utf-8')
    json_data = json.loads(raw_data)
    data = models.FairyDetail.objects.filter(category_id=json_data['categoryId']).values('id', 'name', 'category_id', 'create_time')

    dataResponse = []
    for single in data:
        dataResponse.append(single)
    return JsonResponse(dataResponse, safe=False)

# 通过id查询详情
def fairy_detail_one(request):
    raw_data = request.body.decode('utf-8')
    json_data = json.loads(raw_data)
    data = models.FairyDetail.objects.filter(id=json_data['id']).values('id', 'name', 'category_id', 'create_time', 'content')

    dataResponse = []
    for single in data:
        dataResponse.append(single)
    return JsonResponse(dataResponse, safe=False)
